<template>
	<view class="list-box">
		<view class="head_box">
			<view class="filter-item">
				<sh-filter @change="onFilter" @onConfirm="hanleSort" :dicData="dicData" @onSearch="handleSearch" :perData="personData" @onRanking='handleRanking' :sortList='sortList'></sh-filter>
			</view>
			<view class="nav-wrap">
				<nav-bar :counts="navCounts" :current="navCurrent" @onChange="onNavBarChange"></nav-bar>
			</view>
		</view>
		<view class="content-box">
			<list-item :dataList="listData" :showCheckBox="showChk" @getIndex="getIndex" ref="navchild"
				:currPage="moduleId" />
		</view>
		<mix-float-menu @onBtnClick="clickFloatBtn" @onClose="close" @add="addNewPage" @onWaitsubmit="_showModal"
			:isShow="showItem" ref="child" @onSend="send" @onFinish="handleFinish" @onExport="handleExport"
			@onPause="handlePause" @onDelete="handleDelete" @onWeekSend="handleWeekSend" @onClear="handleClear"
			@onReview="handleReview">
		</mix-float-menu>
		<uni-popup id="popup" ref="popup" type="center" :animation="true">
			<view class="popup-content">
				<view class="content-title">
					纳入本周待提交
				</view>
				<view class="content-name">
					<view class="name-left">
						项目名称
					</view>
					<view class="name-right">
						{{modalName}}
					</view>
				</view>
				<view class="check-box">
					<view class="checkbox-title">
						本周送报类型
					</view>
					<view class="checkbox-cont">
						<radio-group>
							<label>
								<radio value="1" checked="true" />本周新增
							</label>
							<label>
								<radio value="2" />本周有新进展
							</label>
						</radio-group>
					</view>

				</view>
				<view class="btn-grow">
					<button type="default" @click="handleSubmit">确认</button>
					<button type="default" @click="closeModal">返回</button>
				</view>
			</view>
		</uni-popup>
		<mix-export :selectIds="selectIds" :queryString="queryString" @onClose="closeExport" :show="showExport"></mix-export>
	</view>
</template>

<script>
	import {
		request
	} from '../../common/js/request.js'
	import listItem from './components/listItem.vue';
	import shFilter from '../pulic/sh-filter.vue';
	import navBar from './components/pro-nav-bar.vue';
	import mixFloatMenu from '../../components/mix-float-menu.vue';
	import sortBar from "../index/components/min-nav-bar.vue";
	import mixExport from '../../components/mix-export.vue';
	export default {
		components: {
			shFilter,
			navBar,
			listItem,
			mixFloatMenu,
			sortBar,
			mixExport
		},
		data() {
			return {
				navCounts: [], //订单数量
				navs: [{
						name: ''
					},
					{
						name: '本季跟进'
					},
					{
						name: '本季跟进'
					},
				],
				navCurrent: 0, //当前tab
				listData: [],
				personData: [],
				pageNumber: 1,
				moreTip: false,
				showChk: false,
				actIndex: null,
				moduleId: 'busProjManage',
				showItem: 'new,handle,delete,export,cancle,waitsubmit',
				apiName: 'projmanage',
				modalName: '',
				reportType: "1",
				showmenu: false,
				buttons: {},
				dicData: {},
				formData: {
					searchText:''
				},
				sortList:[
					{
						sortField:'CREATE_TIME', //字段名称
						sortOrder:'',//排序方式
						title:'入库日期',  //字段名称
					},
					{
						sortField:'EVALUATE',
						sortOrder:'',//排序方式
						title:'项目星级',
					},
					{
						sortField:'EXPECT_INVEST',
						sortOrder:'',//排序方式
						title:'投资额',
					},
					{
						sortField:'PROJ_START_UP',
						sortOrder:'',//排序方式
						title:'启动时间',
					},
					{
						sortField:'MANAGER_NAME',
						sortOrder:'',//排序方式
						title:'跟进负责人',
					}
				],
				queryString:'',
				selectIds:'',
				showExport:false
			}
		},

		methods: {
			onNavBarChange(e) {
				console.log('e', e)
				this.pageNumber = 1;
				this.listData = []
				if (e == 0) {
					this.moduleId = 'busProjManage'
					this.showItem = 'new,handle,delete,export,cancle,waitsubmit'
					this.apiName = 'projmanage'
					this.reset()
				} else if (e == 1) {
					this.moduleId = 'busProjReportManage'
					this.showItem = 'clear,send'
					this.apiName = 'reportmanage'
					this.reset()
				} else {
					this.moduleId = 'busProjReportAudit'
					this.apiName = 'reportaudit'
					const arr = []
					const buttons = {
						...this.buttons
					}
					if (buttons.clear) {
						arr.push('clear')
					}
					if (buttons.busProjReportAudit) {
						arr.push('review')
					}
					if (buttons.authReport) {
						arr.push('bring')
					}
					console.log('arr', arr)
					this.showItem = arr.length ? arr.join(',') : '';
					this.reset()
				}
				this.getDataList()
			},
			reset() {
				this.pageNumber = 1;
				this.actIndex = null;
				this.showChk = false
				this.showmenu = false
				this.$refs.child.handleClose()
				this.$refs.navchild.reset()
				
			},
			changeReportType(e) {
				console.log('e', e.detail.value)
				this.reportType = e.detail.value
			},
			closeModal() {
				this.$refs.popup.close()
			},
			hanleSort(e) {
				console.log('sort',e)
				const formsData = {...e,...this.formData};
				this.reset()
				formsData.searchText = '';
				this.formData = formsData;
				this.getDataList()
			},
			handleSearch(e){
				console.log('search',e)
				this.reset()
				this.formData.searchText = e;
				this.getDataList()
			},
			handleRanking(data){
				console.log('e',data)
				this.sortList.forEach((item, index) => {
					if(data!==index){
						item.sortOrder=''
					}
				})
				if(this.sortList[data].sortOrder){
					if(this.sortList[data].sortOrder==='asc'){
						this.sortList[data].sortOrder='desc'
					}else{
						this.sortList[data].sortOrder='asc'
					}	
				}else{
					this.sortList[data].sortOrder='asc'
				}
				this.formData.sortField = this.sortList[data].sortField;
				this.formData.sortOrder = this.sortList[data].sortOrder;
				this.getDataList()
			},
			async handleSubmit() {
				const params = {
					datakey: this.listData[this.actIndex].DATAKEY,
					reportType: this.reportType
				}
				const ret = await this.$req({
					url: 'bus/projmanage/addToReport.do',
					data: params
				})
				console.log('ret', ret)
				if (ret.data.success) {
					this.getDataList()
					this.$refs.popup.close()
				} else {
					uni.showToast({
						title: ret.data.messages[0].content,
						icon: "none"
					})
					this.$refs.popup.close()
				}
			},
			onFilter(e) {
				this.goodsList = [];
				this.listParams.page = 1;
			},
			// 路由跳转
			navToPage() {
				uni.navigateTo({
					url: `/pages/projectStorage/projectStorage`
				})
			},
			//新建

			addNewPage() {
				uni.navigateTo({
					url: '../projectManerge/edit/newset'
				})
			},

			//报送
			send() {
				console.log(this.actIndex)

				if (this.actIndex == null) {
					uni.showToast({
						title: "请选择一条数据",
						icon: "none"
					})
					return;
				}
				const datekey = this.listData[this.actIndex].DATAKEY
				const projId = this.listData[this.actIndex].PROJ_ID
				uni.navigateTo({
					url: `../submitReport/submitReport?dataKey=${datekey}&projId=${projId}`
				})
			},

			//办结
			handleFinish() {
				const self = this;
				if (this.actIndex == null) {
					uni.showToast({
						title: "请选择一条数据",
						icon: "none"
					})
					return;
				}
				const datakey = this.listData[this.actIndex].DATAKEY;
				const auth = this.listData[this.actIndex].AUTH;
				const status = this.listData[this.actIndex].PROJ_STATUS;
				uni.showModal({
					title: '确定办结吗',
					async success(e) {
						if (e.confirm) {
							if (auth == 1 && status != 4) {
								uni.showLoading({

								})
								const res = await self.$req({
									url: 'bus/proj/doGetThrough.do',
									data: {
										selectedIds: datakey
									}
								})
								uni.hideLoading()
								if (res.data.success) {
									uni.showToast({
										title: '操作成功'
									})
									self.reset();
									self.getDataList();
								} else {
									uni.showToast({
										title: res.data.messages[0].content,
										icon: 'none'
									})
								}
							} else {
								uni.showToast({
									title: '没有权限操作',
									icon: 'none'
								})
							}
						}
					}
				})
			},
			//删除
			handleDelete() {
				const self = this;
				if (this.actIndex == null) {
					uni.showToast({
						title: "请选择一条数据",
						icon: "none"
					})
					return;
				}
				const datakey = this.listData[this.actIndex].DATAKEY;
				const auth = this.listData[this.actIndex].AUTH;
				const status = this.listData[this.actIndex].PROJ_STATUS;
				uni.showModal({
					title: '确定删除吗?',
					async success(e) {
						if (e.confirm) {
							uni.showLoading({

							})
							const res = await self.$req({
								url: 'bus/projmanage/remove.do',
								data: {
									selectedIds: datakey
								}
							})
							uni.hideLoading()
							if (res.data.success) {
								uni.showToast({
									title: '操作成功'
								})
								self.reset();
								self.getDataList();
							} else {
								uni.showToast({
									title: res.data.messages[0].content,
									icon: 'none'
								})
							}
						}
					}
				})
			},
			//清除 
			async handleClear() {
				const self = this;
				if (this.actIndex == null) {
					uni.showToast({
						title: "请选择一条数据",
						icon: "none"
					})
					return;
				}
				const datakey = this.listData[this.actIndex].DATAKEY;
				const auth = this.listData[this.actIndex].AUTH;
				const status = this.listData[this.actIndex].PROJ_STATUS;
				uni.showModal({
					title: '确定清除吗?',
					async success(e) {
						if (e.confirm) {
							uni.showLoading({

							})
							const res = await self.$req({
								url: 'bus/reportaudit/del.do',
								data: {
									selectedIds: datakey
								}
							})
							uni.hideLoading()
							if (res.data.success) {
								uni.showToast({
									title: '操作成功'
								})
								self.reset();
								self.getDataList();
							} else {
								uni.showToast({
									title: res.data.messages[0].content,
									icon: 'none'
								})
							}
						}
					}
				})
			},
			//导出
			 handleExport() {
				this.showExport = true;
				console.log('this.actins',this.actIndex)
				if(this.actIndex != null){
					const datakey = this.listData[this.actIndex].DATAKEY;
					const auth = this.listData[this.actIndex].AUTH;
					const status = this.listData[this.actIndex].PROJ_STATUS;
					this.selectIds = datakey;
					console.log('this.selectIds===',this.selectIds)
				}
				
				
			},
			closeExport(){
				this.showExport = false
			},
			//暂缓取消暂缓
			async handlePause() {
				const self = this;
				if (this.actIndex == null) {
					uni.showToast({
						title: "请选择一条数据",
						icon: "none"
					})
					return;
				}
				const datakey = this.listData[this.actIndex].DATAKEY;
				const auth = this.listData[this.actIndex].AUTH;
				const status = this.listData[this.actIndex].PROJ_STATUS;
				console.log(auth, status)
				if (auth == 1) {
					uni.showLoading({

					})
					const res = await self.$req({
						url: 'bus/proj/isRepeiee.do',
						data: {
							selectedIds: datakey
						}
					})
					uni.hideLoading()
					if (res.data.success) {
						uni.showToast({
							title: '操作成功'
						})
						self.reset();
						self.getDataList();
					} else {
						uni.showToast({
							title: ret.data.messages[0].content
						})
					}
				} else {
					uni.showToast({
						title: '没有权限操作',
						icon: 'none'
					})
				}
			},
			//纳入本周报送
			async handleWeekSend() {
				const self = this;
				if (this.actIndex == null) {
					uni.showToast({
						title: "请选择一条数据",
						icon: "none"
					})
					return;
				}
				const datakey = this.listData[this.actIndex].DATAKEY;
				const auth = this.listData[this.actIndex].AUTH;
				const status = this.listData[this.actIndex].PROJ_STATUS;
				console.log(auth, status)
				uni.showModal({
					title: '确定纳入本周报送吗?',
					async success(e) {
						if (e.confirm) {
							uni.showLoading({

							})
							const res = await self.$req({
								url: 'bus/reportaudit/pass.do',
								data: {
									selectedIds: datakey,
									moduleId: 'busProjReportAudit',
									status: 2
								}
							})
							uni.hideLoading()
							if (res.data.success) {
								uni.showToast({
									title: '操作成功'
								})
								self.reset();
								self.getDataList();
							} else {
								uni.showToast({
									title: ret.data.messages[0].content
								})
							}
						}
					}
				})
			},
			//组长审核
			handleReview() {
				const self = this;
				if (this.actIndex == null) {
					uni.showToast({
						title: "请选择一条数据",
						icon: "none"
					})
					return;
				}
				const datakey = this.listData[this.actIndex].DATAKEY;
				const auth = this.listData[this.actIndex].AUTH;
				const status = this.listData[this.actIndex].PROJ_STATUS;
				console.log(auth, status)
				uni.showModal({
					title: '确定审核吗?',
					async success(e) {
						if (e.confirm) {
							uni.showLoading({

							})
							const res = await self.$req({
								url: 'bus/reportaudit/pass.do',
								data: {
									selectedIds: datakey,
									moduleId: 'busProjReportAudit',
									status: 1
								}
							})
							uni.hideLoading()
							if (res.data.success) {
								uni.showToast({
									title: '操作成功'
								})
								self.reset();
								self.getDataList();
							} else {
								uni.showToast({
									title: ret.data.messages[0].content
								})
							}
						}
					}
				})
			},
			clickFloatBtn() {
				this.showChk = true
			},
			close() {
				this.showChk = false;
			},
			getIndex(e) {
				console.log('e', e.index)
				this.actIndex = e.index
			},
			//跟进人
			async getFlPerson() {
				const ret = await this.$req({
					url: `bus/${this.apiName}/filter.do`,
					data: {
						moduleId: this.moduleId
					}
				})

				if (ret.data.success) {
					personData = ret.data.attributes.zdyDataSource.personNameDataSourceManage
				}
			},
			_showModal() {
				console.log(this.actIndex)
				if (this.actIndex == null) {
					uni.showToast({
						title: "请选择一条数据",
						icon: "none"
					})
					return;
				}
				this.modalName = this.listData[this.actIndex].NAME
				this.$refs.popup.open()
			},
			async getDataList() {

				uni.showLoading({

				})
				const dicData = JSON.parse(uni.getStorageSync('dicData'));
				const ret = await this.$req({
					url: `bus/${this.apiName}/filter.do`,
					data: {
						moduleId: this.moduleId
					}
				})
				let personData = {}
				if (ret.data.success) {
					personData = ret.data.attributes.zdyDataSource.personNameDataSourceManage
					this.personData = personData
				}
				const basicParams = 
					{
						pageSize: "10", //每页条数
						pageNo: this.pageNumber, //页码
						moduleId: this.moduleId //固定值
					}
				
				const params = {...this.formData,...basicParams}
				if(params.MANAGER == ''){
					delete params.MANAGER
				}
				//列表
				const res = await this.$req({
					url: `bus/${this.apiName}/ajaxSearch.do`,
					data: params
				})
				uni.hideLoading()
				if (res.data.success) {
					const list = this.listData;
					const rows = [...res.data.attributes.rows]

					this.queryString = res.data.attributes.queryString;
					rows.forEach(item => {
						item.industry = item.INDUSTRY ? this.$util.queryDicName(dicData, 'inve_field', item
							.INDUSTRY) : ''
						item.star = parseInt(item.EVALUATE) + 1
						item.createTime = item.CREATE_TIME ? item.CREATE_TIME.split(' ')[0] : ''
						//用地方式
						item.placeUsingType = item.SITE_TYPE ? this.$util.queryDicName(dicData,
							'busProjSiteMethod', item.SITE_TYPE) : ''
						//项目状态
						item.proState = item.PROJ_STATUS ? this.$util.queryDicName(dicData, 'busProjStatus',
							item.PROJ_STATUS) : ''
						//项目
						//跟进人
						item.followPerson = this.$util.queryPersonName(item.MANAGER, personData)
						//项目阶段
						item.proStep = item.PROJ_STAGE ? this.$util.queryDicName(dicData, 'busProjStage', item
							.PROJ_STAGE) : ''
					})
					let newArr = [];
					newArr = this.pageNumber <= 1 ? rows : list.concat(rows)
					this.listData = newArr;
					this.total = res.data.attributes.total
				} else {
					uni.showToast({
						title: res.data.message,
						icon: 'none'
					})
				}
			},
			//获取权限
			async getAuth() {
				const res = await this.$req({
					url: 'bus/reportaudit/filter.do',
					data: {
						moduleId: 'busProjReportAudit'
					}
				})
				if (res.data.success) {
					const {
						buttons
					} = res.data.attributes;
					//本周待审核按钮权限
					this.buttons = buttons;

				}
			},
		
		},
	
		onLoad() {
			this.getAuth()
			this.getDataList()
			if (uni.getStorageSync('dicData')) {
				this.dicData = JSON.parse(uni.getStorageSync('dicData'))
			}
		},
		onShow() {
			this.reset()
		},
		onPullDownRefresh(){
			console.log('下拉刷新')
			this.reset()
			setTimeout(function() {  
				uni.stopPullDownRefresh();  
			}, 1500);  
		},
		onReachBottom() {
			const totalPage = Math.ceil(this.total / 10);
			const currPage = this.pageNumber;
			if (currPage < totalPage) {
				this.pageNumber++;
				this.getDataList()
			} else {
				this.moreTip = true
			}

		},
		onShareAppMessage() {
			return {
				imageUrl: 'https://www.szaez.com/sck/share.jpg?v=' + new Date().getTime()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.head_box {
		position: -webkit-sticky;
		position: sticky;
		top: 0;
		z-index: 998;
		background: #fff;
	}

	.search-box {
		width: 661rpx;
		height: 60rpx;
		background: rgba(245, 245, 245, 1);
		border-radius: 30rpx;
		padding: 0 30rpx;
		// #ifdef MP
		width: 450rpx;

		// #endif
		.search {
			text-align: center;
			font-size: 28rpx;
		}

		.cuIcon-roundclosefill {
			color: #d5a65a;
			padding: 0 10rpx;
		}
	}

	.list-box {
		&:-webkit-scrollbar {
			width: 0;
			height: 0;
			color: transparent;
			display: none;
		}
	}

	.content-box {
		width: 750rpx;

		.nav-wrap {
			width: 750rpx;
			height: 70rpx;
		}
	}

	.goods-list {
		flex-wrap: wrap;

		.goods-item {
			margin-right: 20rpx;
			margin-bottom: 20rpx;

			&:nth-child(2n) {
				margin-right: 0;
			}
		}
	}

	.popup-content {
		width: 680rpx;
		background-color: #fff;
		padding: 20rpx;

		.content-title {
			text-align: center;
			font-size: 36rpx;
		}

		.content-name {
			padding: 40rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.name-left {
			font-size: 28rpx;
			color: #333;
		}

		.name-right {
			font-size: 28rpx;
			color: #999;
		}

		.checkbox-title {
			font-size: 28rpx;
			color: #333;
		}

		.checkbox-cont label {
			font-size: 28rpx;
			color: #999;
		}

		.check-box {
			padding: 40rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.btn-grow {
			padding: 40rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.selectBox {
			background: #EB5248 !important;
			color: #fff !important;
		}

		.checkbox {
			padding: 5px 10px;
			border: 1px solid #EB5248;
			margin: 10px;
			border-radius: 7upx;
			color: #000;
		}
	}
</style>
